package cn.bkw.ytkapi.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;

/**
 * @date 2016-4-25
 * @author tzy
 * @class des数据加密
 */
public class DesUtil {
	// 解密数据
		public static String decrypt(String pwd, String key){
			String aString="";
			try{
				byte[] bytesrc = convertHexString(pwd);
				Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
				DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
				SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
				SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
				IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
	
				cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
	
				byte[] retByte = cipher.doFinal(bytesrc);
				aString = new String(retByte);
			}catch(Exception e){
				
			}
			return aString;
		}

		/**
		 * 加密数据
		 * 
		 * @param message
		 * @param key
		 * @return
		 */
		public static String encrypt(String pwd, String key) {
			try {
				Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");

				DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));

				SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
				SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
				IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
				cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);

				return toHexString(cipher.doFinal(pwd.getBytes("UTF-8")))
						.toUpperCase();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return "";
		}

		public static byte[] convertHexString(String ss) {
			byte digest[] = new byte[ss.length() / 2];
			for (int i = 0; i < digest.length; i++) {
				String byteString = ss.substring(2 * i, 2 * i + 2);
				int byteValue = Integer.parseInt(byteString, 16);
				digest[i] = (byte) byteValue;
			}

			return digest;
		}

		public static String toHexString(byte b[]) {
			StringBuffer hexString = new StringBuffer();
			for (int i = 0; i < b.length; i++) {
				String plainText = Integer.toHexString(0xff & b[i]);
				if (plainText.length() < 2)
					plainText = "0" + plainText;
				hexString.append(plainText);
			}

			return hexString.toString();
		}
		
		// 给数组加密
        public static String DESEncryptArr(String strList, String decryptKey, String chr)
        {
        	String result = "";
        	String spite="";
        	if (chr.equals("|")) {
				spite="\\|";
			}else {
				spite=chr;
			}
        	String[] aryList = strList.split(spite);
            for (int i = 0; i < aryList.length; i++)
            {
                if (result.equals("")||result==null)
                {
                    result = encrypt(aryList[i].toString(), decryptKey);
                }
                else
                {
                    result += chr + encrypt(aryList[i].toString(), decryptKey);
                }
            }
            return result;
        }
        /// 给数组解密
        /// </summary>
        /// <param name="strList">数组字符串</param>
        /// <param name="decryptKey">key</param>
        /// <param name="chr">数组分隔符</param>
        /// <returns></returns>
        public static String DESDecryptArr(String strList, String decryptKey, String chr)
        {
        	String result = "";
        	String[] aryList = strList.split(chr);
            for (int i = 0; i < aryList.length; i++)
            {
                if (result.equals("")||result==null)
                {
                    result = decrypt(aryList[i].toString(), decryptKey);
                }
                else
                {
                    result += chr + decrypt(aryList[i].toString(), decryptKey);
                }
            }
            return result;
        }
}
